package cn.wolfcode.consumer._04_consumeway;

import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.PullResult;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;

/**
 * 拉取消费
 */
public abstract class PullConsumer {
    public static void main(String[] args) throws Exception {
        //1.创建消费者对象
        DefaultMQPullConsumer pullConsumer = new DefaultMQPullConsumer("wolfcode-consumer");
        //2.设置消费者对象的名字服务
        pullConsumer.setNamesrvAddr("127.0.0.1:9876");
        //3.启动消费者
        pullConsumer.start();
        //4.主动去broker中拉取消息
        PullResult pullResult = pullConsumer.pull(new MessageQueue("06-consumeway-boot", "broker-a", 0), "*", 0, 10);
        //5.拉取结果中拿到想要的数据:列表的集合
        List<MessageExt> list = pullResult.getMsgFoundList();
        for (MessageExt messageExt : list) {
            System.out.println("消费线程:"+Thread.currentThread().getName()+",消息ID:"+messageExt.getMsgId()+",消息内容:"+new String(messageExt.getBody()));
        }
        //6.关闭拉取消费
        pullConsumer.shutdown();
    }

}
